package com.example.administrator.reader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.reader.R;
import com.example.administrator.reader.activity.LoginActivity;
import com.example.administrator.reader.activity.SettingActivity;
import com.example.administrator.reader.utils.Constants;
import com.example.administrator.reader.utils.UIUtils;


/**
 * Created by Administrator on 2016-09-04.
 */
public class UserFragment extends BaseFragment {

    private LinearLayout layoutLogin;
    private TextView tv_user_id;
    private ImageView iv_head;
    private LinearLayout layout_setting;

    // 判断是否已经登录
    static public boolean isLogin = false;

    // 用户账号
    public static String USER_ACCOUNT;

    @Override
    protected void setUpContentView() {
        view = View.inflate(UIUtils.getContext(), R.layout.f_user, null);
    }

    @Override
    protected void setUpView() {
        layoutLogin = (LinearLayout) view.findViewById(R.id.layout_login);
        tv_user_id = (TextView) view.findViewById(R.id.tv_user_id);
        iv_head = (ImageView) view.findViewById(R.id.iv_head);
        layout_setting = (LinearLayout) view.findViewById(R.id.layout_setting);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        layoutLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogin) {
                    startActivityForResult(new Intent(getActivity(), LoginActivity.class), Constants.requestCode);
                }
            }
        });
        layout_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getActivity(), SettingActivity.class), Constants.requestCodeLogout);
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.requestCode && resultCode == Constants.resultCode){
            if (data != null){

                USER_ACCOUNT = data.getStringExtra("account");

                tv_user_id.setText(data.getStringExtra("account"));
                iv_head.setImageResource(R.drawable.head);
                isLogin = true;
            }
        }else if (requestCode == Constants.requestCodeLogout && resultCode == Constants.resultCodeLogout){
            tv_user_id.setText("点击登录");
            iv_head.setImageResource(R.drawable.user_face_defaut);
            isLogin = false;
        }
    }


    @Override
    public void loadData() {
    }

    @Override
    public void reloadData() {

    }

}
